package com.zl.view;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;

import com.zl.dao.BookBorrowDao;
import com.zl.dao.MessageDao;
import com.zl.model.BookBorrow;
import com.zl.model.Message;
import com.zl.util.ZLString;
import com.zl.util.ZLUtil;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MessageBoardFrame extends JInternalFrame {
	private JTable table;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MessageBoardFrame frame = new MessageBoardFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MessageBoardFrame() {
		setClosable(true);
		setIconifiable(true);
		setTitle("留言板");
		setBounds(100, 100, 556, 420);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel label = new JLabel("留言内容：");
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		JButton button = new JButton("写留言");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				writeMessage(e);
			}
		});
		button.setIcon(new ImageIcon(MessageBoardFrame.class.getResource("/images/edit.png")));
		
		JButton button_1 = new JButton("重置");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetMessageInfo(e);
			}
		});
		button_1.setIcon(new ImageIcon(MessageBoardFrame.class.getResource("/images/reset.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 439, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(16, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(167)
					.addComponent(button)
					.addGap(65)
					.addComponent(button_1)
					.addContainerGap(173, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 267, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(label)
						.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(button_1)
						.addComponent(button))
					.addContainerGap())
		);
		
		textArea = new JTextArea();
		scrollPane_1.setViewportView(textArea);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"\u7F16\u53F7", "\u7559\u8a00\u7528\u6237", "\u7559\u8a00\u5185\u5bb9", "\u7559\u8a00\u65f6\u95f4"
				}
			) {
				boolean[] columnEditables = new boolean[] {
					false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
		table.getColumnModel().getColumn(0).setPreferredWidth(20);
		table.getColumnModel().getColumn(1).setPreferredWidth(50);
		table.getColumnModel().getColumn(2).setPreferredWidth(119);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);
		
		this.fillTable();
	}
	
	/**
	 * 重置信息
	 * @param e
	 */
	private void resetMessageInfo(ActionEvent e) {
		// TODO Auto-generated method stub
		this.fillTable();
		this.textArea.setText("");
	}

	/**
	 * 写留言
	 * @param e
	 */
	private void writeMessage(ActionEvent e) {
		// TODO Auto-generated method stub
		String messageContent = this.textArea.getText();
		if (ZLString.isEmpty(messageContent)) {
			JOptionPane.showMessageDialog(null, "请填写留言信息！");
			return;
		}
		int userId = LoginFrame.loginSuccessUser.getId();
		String messageTime = (long)System.currentTimeMillis() + "";
		String userName = LoginFrame.loginSuccessUser.getUserName();
		
		Message message = new Message(userId, userName, messageContent, messageTime);
		Connection connection = null;
		
		try {
			
			connection = ZLUtil.getConnect();
			int success = MessageDao.add(connection, message);
			
			if (success == 1) {
				
				JOptionPane.showMessageDialog(null, "留言成功！");
				
				//重置页面信息
				this.textArea.setText("");
				this.fillTable();
			}else {

				JOptionPane.showMessageDialog(null, "留言失败！");
			}
			
		} catch (Exception e2) {
			// TODO: handle exception
			e2.printStackTrace();
			JOptionPane.showMessageDialog(null, "留言失败！");
		} finally {
			try {
				ZLUtil.colseConnect(connection);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 初始化表格数据
	 * @param book
	 */
	private void fillTable(){
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0); // 设置成0行 就是清空数据

		Connection connection = null;
		try{
			connection = ZLUtil.getConnect();
			ResultSet set = MessageDao.select(connection);
			while(set.next()){
				
				Vector v=new Vector();
				v.add(set.getInt("id"));
				v.add(set.getString("userName"));
				v.add(set.getString("messageContent"));
				//对时间做处理
				String time = set.getString("messageTime");
				v.add(ZLString.timeStamp2Date(time));
				dtm.addRow(v);

			}
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			try {
				ZLUtil.colseConnect(connection);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
